<?php


namespace app\business\controller;


use think\Db;
use think\Request;

class Fund extends Base
{
    // 总流水
    public function totalFlows(Request $request)
    {
        $crumbs['oneMenuName'] = '平台资金';
        $crumbs['twoMenuName'] = '总流水';

        $business = db('saas_business')->where('status',1)->select();

        $join = [
            [['app_saas_business' => 'b'],'a.b_id=b.id'],
            [['app_saas_order' => 'c'],'a.order_code=c.order_code'],
        ];

        $search = $request->get('search');
        $map = [];
        $where = [];
        if ($search){
            $where['a.b_id'] = $search;
            $map['query']['search'] = $search;
        }

        $where['a.status'] = ['in','2,3,4'];

        $data = db('saas_b_order')->alias('a')
            ->field('a.*, b.name b_name, c.pay_time')
            ->join($join)
            ->where($where)
            ->order('a.id', 'desc')
            ->paginate(10, false, $map);

        return $this->fetch('', compact('crumbs', 'business', 'data'));
    }
    
    
    // 待放款
    public function toBeLent(Request $request)
    {
        $crumbs['oneMenuName'] = '平台资金';
        $crumbs['twoMenuName'] = '待放款';

        $search = $request->get('search');
        $map = [];
        $where = [];
        if ($search){
            $where['a.b_id'] = $search;
            $map['query']['search'] = $search;
        }

        $where['b.status'] = 1;

        $join = [
            ['saas_business b','a.b_id=b.id'],
        ];
        $data = db('saas_withdrawal')->alias('a')
            ->field('a.*, b.name b_name')
            ->join($join)
            ->where($where)
            ->order('a.id', 'desc')
            ->paginate(10, false, $map);

        $business = db('saas_business')->where('status',1)->select();

        return $this->fetch('', compact('crumbs', 'business', 'data'));
    }

    // 放款
    public function putWithdrawal(Request $request)
    {
        if ($request->isPost()){
            $id = input('id');
            $detaileds_id_str = db('saas_withdrawal')->where('id', $id)->value('detaileds_id');

            if ($detaileds_id_str){
                Db::startTrans();
                $order_result = db('saas_b_order')->whereIn('id', $detaileds_id_str)->update(['is_withdrawal' => 2]);
                if ($order_result !== false){

                    $save['id'] = $id;
                    $save['status'] = 2;
                    $save['put_time'] = time();
                    $withdrawal_result = db('saas_withdrawal')->update($save);
                    if ($withdrawal_result){
                        Db::commit();
                        $ajax['code'] = 1;
                        $ajax['msg'] = '操作成功';
                    }else{
                        Db::rollback();
                        $ajax['msg'] = '放款失败';
                    }
                }else{
                    Db::rollback();
                    $ajax['msg'] = '放款失败';
                }
            }else{
                $ajax['msg'] = '放款失败';
            }
            return json($ajax);
        }
    }


    // 商家财务详情
    public function businessFinance(Request $request)
    {

        $crumbs['oneMenuName'] = '平台资金';
        $crumbs['twoMenuName'] = '商家财务详情';

        $search = $request->get('search');
        $map = [];
        $where = [];
        if ($search){
            $where['a.b_id'] = $search;
            $map['query']['search'] = $search;
        }

        $join = [
            ['saas_business b','a.b_id=b.id'],
        ];

        // 待优化
        $data = db('saas_b_order')->alias('a')
            ->field('a.*, b.name b_name')
            ->join($join)
            ->where($where)
            ->order('a.id', 'desc')
            ->group('a.b_id')
            ->paginate(10, false, $map);

        $_data = db('saas_b_order')->alias('a')
            ->field('a.*, b.name b_name')
            ->join($join)
            ->where($where)
            ->order('a.id', 'desc')
            ->paginate(10, false, $map);

        // 当前时间 7天之前的收货时间
        $take_time = strtotime('-7 day');

        $list_data = [];

        $status_arr = [1,5,6];

        // 待优化
        foreach($_data as $k => $v){
            $price = $v['goods_number'] * $v['goods_price'];
            if (!isset($list_data[$v['b_id']])){
                $list_data[$v['b_id']]['b_name'] = $v['b_name'];
                // 已提现
                if ($v['is_withdrawal'] == 2){
                    $list_data[$v['b_id']]['yitixian'] = $price;
                }
                //待提现
                if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr)){
                    $list_data[$v['b_id']]['daitixian'] = $price;
                }
                //可提现
                //if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr) && ($take_time > $v['take_time'])){
                //    $list_data[$v['b_id']]['ketixian'] = $price;
                //}
                if ($v['is_withdrawal'] == 1 && $v['status'] == 4){
                    $list_data[$v['b_id']]['ketixian'] = $price;
                }
                //不可提现
                //if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr) && ($take_time < $v['take_time'])){
                //    $list_data[$v['b_id']]['butixian'] = $price;
                //}
                if ($v['is_withdrawal'] == 1 && $v['status'] == 3){
                    $list_data[$v['b_id']]['butixian'] = $price;
                }
            }else{
                // 已提现
                if ($v['is_withdrawal'] == 2){
                    $list_data[$v['b_id']]['yitixian'] += $price;
                }
                //待提现
                if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr)){
                    $list_data[$v['b_id']]['daitixian'] += $price;
                }
                //可提现
                //if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr) && ($take_time > $v['take_time'])){
                //    $list_data[$v['b_id']]['ketixian'] += $price;
                //}
                if ($v['is_withdrawal'] == 1 && $v['status'] == 4){
                    $list_data[$v['b_id']]['ketixian'] += $price;
                }
                //不可提现
                //if ($v['is_withdrawal'] == 1 && !in_array($v['status'], $status_arr) && ($take_time < $v['take_time'])){
                //    $list_data[$v['b_id']]['butixian'] += $price;
                //}
                if ($v['is_withdrawal'] == 1 && $v['status'] == 3){
                    $list_data[$v['b_id']]['butixian'] += $price;
                }
            }
        }


        $business = db('saas_business')->where('status',1)->select();

        return $this->fetch('', compact('crumbs', 'business', 'data', 'list_data'));
    }
}